export const getFile = () => {
  // 请求本地文件系统对象，将服务器链接写入文件进行本地化保存
  plus.io.requestFileSystem(
    plus.io.PUBLIC_DOCUMENTS, // 程序公用文档目录常量
    (fs) => {
      // console.log(JSON.parse(JSON.stringify(fs.root)))
      // 创建或打开文件, fs.root是根目录操作对象,直接fs表示当前操作对象
      fs.root.getFile(
        "/storage/emulated/0/config.json",
        {
          create: false, // 文件不存在则创建
        },
        (fileEntry) => {
          // 文件在手机中的路径
          fileEntry.createWriter(
            (writer) => {
              writer.onwrite = function (e) {
                console.log("Write data success!");
              };
              // Write data to the end of file.
              // writer.seek(1); //控制从哪开始覆盖
              let DataString = JSON.stringify("testestetetste");
              console.log(DataString);
              writer.write(DataString); //将修改后的文件写入手机获取到的文件内
            },
            function (e) {
              console.log("写入失败");
            }
          );
        }
      );
    },
    (e) => {
      console.log(e.message);
    }
  );
};

export const test = () => {
  console.log('test: ')

  // 震动
  plus.device.vibrate(500)
  //声音
  let main = plus.android.runtimeMainActivity()
  let RingtoneManager = plus.android.importClass(
    'android.media.RingtoneManager'
  )
  let uri = RingtoneManager.getActualDefaultRingtoneUri(
    main,
    RingtoneManager.TYPE_NOTIFICATION
  )
  let MediaPlayer = plus.android.importClass('android.media.MediaPlayer')
  let player = MediaPlayer.create(main, uri)
  player.setLooping(false)
  player.prepare()
  player.start()
}

// 设置闹钟
export const setAlarm = (hour, minutes, message) => {
<<<<<<< HEAD
  console.log('setAlarm: ')

  var Intent = plus.android.importClass('android.content.Intent')
  var AlarmClock = plus.android.importClass('android.provider.AlarmClock')
  var mIntent = new Intent(AlarmClock.ACTION_SET_TIMER)
=======
  console.log("setAlarm: ");
  const pathArr = JSON.parse(plus.storage.getItem("localData"));
  const randomIndex = Math.floor(Math.random() * pathArr.length);
  const randomUrl = pathArr[randomIndex].url;
  console.log("randomIndex: ", randomIndex, randomUrl);
  const ringtoneUri = randomUrl;
  var Intent = plus.android.importClass("android.content.Intent");
  var AlarmClock = plus.android.importClass("android.provider.AlarmClock");
  var intent = new Intent(AlarmClock.ACTION_SET_ALARM);
>>>>>>> 97615a99fe9d1d00d10110b076f438f0e843a268

  // var mIntent = new Intent('android.settings.SOUND_SETTINGS')

  // var main = plus.android.runtimeMainActivity()
  // main.startActivity(mIntent)

  const date = new Date()
  // 闹钟的小时
<<<<<<< HEAD
  mIntent.putExtra(AlarmClock.EXTRA_HOUR, date.getHours())
  //闹钟的分钟
  mIntent.putExtra(AlarmClock.EXTRA_MINUTES, date.getMinutes() + 1)
  //响铃时提示的信息
  mIntent.putExtra(AlarmClock.EXTRA_MESSAGE, '小楼一夜听春雨')

  var context = plus.android.importClass('android.content.Context')
  console.log('context: ', context)
  // getSystemService(context.ALARM_SERVICE)
=======
  intent.putExtra(AlarmClock.EXTRA_HOUR, hour);
  //闹钟的分钟
  intent.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
  //响铃时提示的信息
  intent.putExtra(AlarmClock.EXTRA_MESSAGE, message);
>>>>>>> 97615a99fe9d1d00d10110b076f438f0e843a268

  //对于一次性闹铃，无需指定此 extra
  //一个 ArrayList，其中包括应重复触发该闹铃的每个周日。
  // 每一天都必须使用 Calendar 类中的某个整型值（如 MONDAY）进行声明。
  //例如[this.Calendar.MONDAY,this.Calendar.TUESDAY,this.Calendar.WEDNESDAY]
<<<<<<< HEAD
  // mIntent.putExtra(AlarmClock.EXTRA_DAYS, [
  //   'SUNDAY',
  //   'MONDAY',
  //   'TUESDAY',
  //   'WEDNESDAY',
  //   'THURSDAY',
  //   'FRIDAY',
  //   'SATURDAY',
  // ])

  //用于指定该闹铃触发时是否振动
  // mIntent.putExtra(AlarmClock.EXTRA_VIBRATE, true)
=======
  var Calendar = plus.android.importClass("java.util.Calendar");
  // intent.putExtra(AlarmClock.EXTRA_DAYS, [
  //   Calendar.MONDAY,
  //   Calendar.TUESDAY,
  //   Calendar.WEDNESDAY,
  // ]);

  //一个 content: URI，用于指定闹铃使用的铃声，也可指定 VALUE_RINGTONE_SILENT 以不使用铃声。
  //如需使用默认铃声，则无需指定此 extra。
  intent.putExtra(AlarmClock.EXTRA_RINGTONE, ringtoneUri);
>>>>>>> 97615a99fe9d1d00d10110b076f438f0e843a268

  //用于指定该闹铃触发时是否振动
  intent.putExtra(AlarmClock.EXTRA_VIBRATE, true);
  //如果为true，则调用startActivity()不会进入手机的闹钟设置界面
<<<<<<< HEAD
  // mIntent.putExtra(AlarmClock.EXTRA_SKIP_UI, true)

  // 清空任务中在其之上的Activity
  // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

  var main = plus.android.runtimeMainActivity()
  plus.android.importClass('android.app.Activity')

  main.startActivity(mIntent)
  // if (intent.resolveActivity(main.getPackageManager()) != null) {
  // }
}
=======
  intent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
  // 清空任务中在其之上的Activity
  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  plus.android.importClass("android.app.Activity");
  var main = plus.android.runtimeMainActivity();

  if (intent.resolveActivity(main.getPackageManager()) != null) {
    main.startActivity(intent);
  }
};
>>>>>>> 97615a99fe9d1d00d10110b076f438f0e843a268

// 设置闹钟
export const setAlarm1 = (hour, minutes, message) => {
  console.log("setAlarm1: ");
  var main = plus.android.runtimeMainActivity();
  // 广播接收
  var ALARM_RECEIVER = "alarm_receiver";
  var receiver = plus.android.implements(
    "io.dcloud.android.content.BroadcastReceiver",
    {
      onReceive: function (context, intent) {
        //实现onReceiver回调函数
        plus.android.importClass(intent); //通过intent实例引入intent类，方便以后的‘.’操作
        //console.log(intent.getAction()); //获取action
        var txt = "接收到消息" + intent.getAction() + ": " + getCurTime();
        // mui.toast(txt)
        pushMsg(txt);
        main.unregisterReceiver(receiver); //取消监听
      },
    }
  );

  // --- 注册监听 start ---
  var IntentFilter = plus.android.importClass("android.content.IntentFilter");
  var filter = new IntentFilter(ALARM_RECEIVER);
  main.registerReceiver(receiver, filter);
  // --- 注册监听 end ---
  // --- 设置闹铃时间 start ---
  var Calendar = plus.android.importClass("java.util.Calendar");
  var calendar = Calendar.getInstance();
  // 11点4分执行
  //calendar.set(Calendar.HOUR_OF_DAY, hour);
  //calendar.set(Calendar.MINUTE, minutes);
  //calendar.set(Calendar.SECOND, 6);
  // 过5s 执行
  calendar.setTimeInMillis(Date.parse(new Date()));
  calendar.add(Calendar.SECOND, 5);
  // --- 设置闹铃时间 end ---
  // --- 设置闹铃 ---
  var Intent = plus.android.importClass("android.content.Intent");
  var intent = new Intent(ALARM_RECEIVER);
  //
  var PendingIntent = plus.android.importClass("android.app.PendingIntent");
  var sender = PendingIntent.getBroadcast(main, 0, intent, 0);
  //
  var AlarmManager = plus.android.importClass("android.app.AlarmManager");
  var Context = plus.android.importClass("android.content.Context");
  var alarm = main.getSystemService(Context.ALARM_SERVICE);
  // 一次
  alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
  // 重复多次
  alarm.setRepeating(
    AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(),
    5 * 1000,
    sender
  );
  // mui.toast('设置闹铃5秒后提醒: ', getCurTime())

  function pushMsg(content) {
    var options = {
      title: "闹铃提醒",
    };
    plus.push.createMessage(content, "alarm", options);
  }
  //
  function getCurTime() {
    var d = new Date();
    return (
      d.getFullYear() +
      "-" +
      (d.getMonth() + 1) +
      "-" +
      d.getDate() +
      " " +
      d.getHours() +
      ":" +
      d.getMinutes() +
      ":" +
      d.getSeconds()
    );
  }
};

// 获取手机的默认铃声震动
export const msg_remind = (type) => {
  console.log("type: ", type);
  if (type == "vibrate") {
    //震动
    plus.device.vibrate(500);
  } else {
    //声音
    let main = plus.android.runtimeMainActivity();
    let RingtoneManager = plus.android.importClass(
      "android.media.RingtoneManager"
    );
    let uri = RingtoneManager.getActualDefaultRingtoneUri(
      main,
      RingtoneManager.TYPE_NOTIFICATION
    );
    console.log("uri: ", uri);
    var Intent = plus.android.importClass("android.content.Intent");
    var AlarmClock = plus.android.importClass("android.provider.AlarmClock");
    var intent = new Intent(AlarmClock.ACTION_SET_ALARM);
    main.startActivity(intent);
    console.log("intent: ", intent);

    let MediaPlayer = plus.android.importClass("android.media.MediaPlayer");
    let player = MediaPlayer.create(main, uri);
    player.setLooping(false);
    player.prepare();
    player.start();
  }
};
